<template>
  <!-- 添加或修改通关全流程业务流程模板对话框 -->
  <el-dialog :title="title" :visible.sync="dialogVisible" width="1000px" append-to-body :close-on-click-modal="false"
    @close="close">
    <el-form ref="form" :model="form" :rules="rules" label-width="98px" label-position="right" class="flex-form"
      size="small">
      <el-form-item label="模板名称" prop="templateName">
        <el-input v-model.trim="form.templateName" placeholder="请输入模板名称" />
      </el-form-item>
      <el-form-item label="进出口类型" prop="iEFlag">
        <el-select v-model.trim="form.iEFlag" filterable placeholder="请选择进出口类型">
          <el-option v-for="dict in dict.type.sys_i_e_flag" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="通关代码" prop="customsStateCode">
        <el-select v-model.trim="form.customsStateCode" filterable placeholder="请选择通关代码">
          <el-option v-for="dict in dict.type.mft_biz_mode" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="业务类型" prop="businessType">
        <el-select v-model.trim="form.businessType" filterable placeholder="请选择业务类型">
          <el-option v-for="dict in dict.type.sys_bill_business_type" :key="dict.value" :label="dict.label"
            :value="dict.value"></el-option>
        </el-select>
      </el-form-item>
    </el-form>

    <el-divider content-position="center">流程配置</el-divider>
      <el-form ref="stepform" :model="stepform" :rules="stepRules" label-width="98px" label-position="right" class="flex-form"
        size="small">

        <el-form-item label="步骤" prop="stepCode">
          <el-select v-model.trim="stepform.stepCode" multiple filterable placeholder="请选择步骤">
            <el-option v-for="dict in dict.type.mft_vehicle_work_flow" :key="dict.value" :label="dict.label"
              :value="dict.value"></el-option>
          </el-select>
        </el-form-item>
        <el-button type="primary" @click="submitStepForm">添加</el-button>
        <el-button type="primary" @click="clearStepList">清空</el-button>
      </el-form>


     <el-table
          :data="stepList"
          style="width: 100%">
          <el-table-column
            prop="stepCode"
            label="步骤"
            width="180">
            <template slot-scope="scope">
              <dict-tag :options="dict.type.mft_vehicle_work_flow" :value="scope.row.stepCode"/>
            </template>
          </el-table-column>
          <el-table-column
            prop="sort"
            label="排序">
          </el-table-column>
          <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right" >
            <template slot-scope="scope">
              <el-button
                size="mini"
                type="text"
                icon="el-icon-delete"
                @click="deleteStepRow(scope.$index)"
              >删除</el-button>
            </template>
          </el-table-column>
        </el-table>


    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="submitForm">确 定</el-button>
      <el-button @click="close">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
  import {
    getMftVehicleWorkflowTemplate,
    addMftVehicleWorkflowTemplate,
    updateMftVehicleWorkflowTemplate
  } from "@/api/billsdecl/mftVehicleWorkflowTemplate";
  export default {
    name: "MftVehicleWorkflowTemplate-addEdit",
    props: {
      dialogType: {
        type: String,
        default: "add",
      },
      rowData: {
        type: Object,
        default: () => {},
      },
      dict: {}
    },
    data() {
      return {
        // 弹出层
        dialogVisible: false,
        // 标题
        title: "新增",
        // 表单参数
        form: {
          id: null,
          templateCode: null,
          templateName: null,
          iEFlag: null,
          customsStateCode: null,
          businessType: null,
          createBy: null,
          createTime: null,
          updateBy: null,
          updateTime: null

        },
        // 通关全流程业务流程模板表格数据
        mftVehicleWorkflowTemplateList: [],
        stepList:[],
        //初始排序
         sortInit:1,
        // 表单校验
        rules: {
          templateName: [{
            required: true,
            message: "模板名称不能为空",
            trigger: "blur"
          }],
          iEFlag: [{
            required: true,
            message: "进出口类型不能为空",
            trigger: "change"
          }],
          customsStateCode: [{
            required: true,
            message: "通关代码不能为空",
            trigger: "change"
          }],
          businessType: [{
            required: true,
            message: "业务类型不能为空",
            trigger: "change"
          }],
        },
        stepform: {
          stepCode: null,
          sort: null,
        },
        stepRules: {
          stepCode: [{
            required: true,
            message: "流程不能为空",
            trigger: "blur"
          }],
        }
      };
    },
    methods: {
      open() {
        this.dialogVisible = true;
        this.$nextTick(() => {
          if (this.dialogType == "add") {
            this.title = "新增";
          } else if (this.dialogType == "edit") {
            this.title = "编辑";
            this.loadDetail();
          } else if (this.dialogType == "detail") {
            this.title = "详情";
            this.loadDetail();
          }
        });
      },
      close() {
        this.$refs["form"].resetFields();
        this.dialogVisible = false;
      },

      // 获取详情
      loadDetail() {
        const id = this.rowData.id;
        getMftVehicleWorkflowTemplate(id).then((res) => {
          if (res.code == 200) {
            this.form = res.data;
          }
        });
      },

      submitStepForm(){
        this.$refs["stepform"].validate(valid => {
          if (valid) {
            this.stepform.stepCode.forEach(item=>{
              let params = {stepCode:item,sort:this.sortInit++};
              this.stepList.push(params);
            });
            this.$refs["stepform"].resetFields();
          }
        });
      },

      //删除步骤
    deleteStepRow(index) {
      this.stepList.splice(index, 1);
      this.sortInit--;
    },
    clearStepList(){
      this.stepList=[];
      this.sortInit=1;
    },


      // 提交表单
      submitForm() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            let params = {
              ...this.form,
            };
            if(this.stepList.length==0){
               this.$modal.msgSuccess("步骤不能为空");
               return;
            }
            params.stepsList=this.stepList;
            if (this.dialogType == "add") {
              addMftVehicleWorkflowTemplate(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("新增成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            } else {
              params.id = this.rowData.id;
              updateMftVehicleWorkflowTemplate(params).then(res => {
                if (res.code === 200) {
                  this.$modal.msgSuccess("修改成功");
                  this.close();
                  this.$emit("handleQuery");
                }
              });
            }
          }
        });
      },
    },
  };
</script>

<style lang="scss" scoped></style>
